package com.situ.gym.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Reserve;
import com.situ.gym.pojo.query.ReserveQuery;
import com.situ.gym.pojo.vo.ReserveVO;
import com.situ.gym.service.IReserveService;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 预约管理 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-08-22
 */
@RestController
@RequestMapping("/reserve")
public class ReserveController {
    
    @Autowired
    private IReserveService reserveService;

    /**
     * 分页查询预约列表
     */
    @GetMapping("/list")
    public Result list(ReserveQuery reserveQuery) {
        System.out.println("ReserveController.list");
        IPage<ReserveVO> page = reserveService.list(reserveQuery);
        return Result.ok(page);
    }

    /**
     * 根据ID删除预约
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        reserveService.removeById(id);
        return Result.ok("删除成功");
    }

    /**
     * 批量删除预约
     */
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Long[] ids) {
        reserveService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    /**
     * 添加预约
     */
    @PostMapping("/add")
    public Result add(@RequestBody Reserve reserve) {
        System.out.println(reserve);
        reserveService.save(reserve);
        return Result.ok("添加成功");
    }

    /**
     * 根据ID查询预约
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id) {
        Reserve reserve = reserveService.getById(id);
        return Result.ok(reserve);
    }

    /**
     * 更新预约
     */
    @PutMapping("/update")
    public Result update(@RequestBody Reserve reserve) {
        System.out.println(reserve);
        reserveService.updateById(reserve);
        return Result.ok("更新成功");
    }

    /**
     * 更新预约状态
     */
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Reserve reserve = new Reserve();
        reserve.setId(id);
        reserve.setStatus(status);
        reserveService.updateById(reserve);
        return Result.ok("修改成功");
    }

    /**
     * 取消预约
     */
    @PutMapping("/cancel/{id}")
    public Result cancelReserve(@PathVariable Long id) {
        System.out.println("管理员取消预约 - ID: " + id);
        Reserve reserve = new Reserve();
        reserve.setId(id);
        reserve.setStatus(0); // 0表示已取消
        reserveService.updateById(reserve);
        System.out.println("预约已取消 - ID: " + id);
        return Result.ok("预约已取消");
    }
}